$(function () {
    var dateRangePicker = $('#daterangepicker');
    var initDateRangePicker = function () {
        var rangePast = {
            '今天': [moment(), moment()],
            '昨天': [moment().subtract(1,'days'), moment().subtract(1,'days')],
            '最近7天': [moment().subtract(6,'days'), moment()],
            '最近30天': [moment().subtract(29,'days'), moment()],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '上个月': [moment().subtract(1,'month').startOf('month'), moment().subtract(1,'month').endOf('month')]
        };
        var rangeFuture = {
            '今天': [moment(), moment()],
            '未来7天': [moment(), moment().add(6,'days')],
            '未来30天': [moment(), moment().add(29,'days')],
            '未来60天': [moment(), moment().add(59,'days')],
            '本月': [moment().startOf('month'), moment().endOf('month')],
            '下个月': [moment().add(1,'month').startOf('month'), moment().add(1,'month').endOf('month')]
        };
        
        dateRangePicker.daterangepicker({
            ranges: rangePast,
            startDate: moment().subtract(29,'days'),
            endDate: moment(),
            applyClass: "btn-primary btn-block",
            cancelClass: "btn-block",
            format: "YYYY-MM-DD",
            locale: {
                applyLabel: '确定',
                clearLabel: "取消",
                fromLabel: '开始时间',
                toLabel: '结束时间',
                weekLabel: '周',
                customRangeLabel: '选择范围',
                daysOfWeek: "日_一_二_三_四_五_六".split("_"),
                monthNames: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
                firstDay: 0
            },
            opens: "left"
        });

        var formatDate = function (date) {
            return $.format.date(new Date(date), "yyyy/MM/dd");
        };
        // 根据状态设定初始日期
        var defaultDate = formatDate(moment().subtract(29,'days')) + ' - ' + formatDate(moment());
        dateRangePicker.val(defaultDate);
        $(".daterangepicker .ranges ul li:last").hide();
        $(".daterangepicker .ranges .daterangepicker_start_input").remove();
        $(".daterangepicker .ranges .daterangepicker_end_input").remove();
        $(".daterangepicker .ranges .cancelBtn").remove();
    };

    var initTable = function () {
        var table = $('#pointsList').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "points/getList",
                "data": function (d) {
                    var dateRange = dateRangePicker.val().split(" - ");
                    d.startTime = new Date(dateRange[0])
                    var endTime = moment(dateRange[1], "YYYY-MM-DD").endOf('day');
                    d.endTime = new Date(endTime);
                },
                "type": "POST"
            },
            "columnDefs": [{
                    "targets": 0, //用户名
                    "mRender": function (data, type, row) {
                        return '<a href="user/index/' + row.userId + '" target="_blank">' + row.loginName + '</a>';
                    }
                }, {
                    "targets": 1, //日期
                    "mRender": function (data, type, row) {
                        return $.format.date(new Date(row.recordTime), 'yyyy-MM-dd HH:mm:ss');;
                    }
                }, {
                    "targets": 2, //通过渠道
                    "mRender": function (data, type, row) {
                        return sysPointType[row.pointTypeId];
                    }
                }, {
                    "targets": 3, //获取商品
                    "mRender": function (data, type, row) {
                        return row.commodityName;
                    }
                }, {
                    "targets": 4, //消耗积分
                    "mRender": function (data, type, row) {
                        return row.points;
                    }
                }, {
                    "targets": 5, //商品状态
                    "mRender": function (data, type, row) {
                        if(row.address === null){
                            return "n/a";
                        }else{
                            return row.expressNumbers === null ? "未发货":"已发货";
                        }
                    }
                }, {
                    "targets": 6, //快递单号
                    "mRender": function (data, type, row) {
                        if(row.address === null){
                            return "n/a";
                        }else{
                            return row.expressNumbers === null ? "<span class='express' style='color:#FF0000;width:184px;height:19px;display:inline-block;cursor:pointer' id='"+row.id+"'>添加快递单号</span>" : row.expressNumbers;
                        }
                    }
                }, {
                    "targets": 7, //地址详情
                    "mRender": function (data, type, row) {
                        return row.address === null ? "n/a" : row.address;
                    }
                }],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ","
            },
            "iDisplayLength": 20,
            "aLengthMenu": [
                [5, 10, 20, 50, -1],
                [5, 10, 20, 50, "全部"]
            ],
            "sDom": 't<"dt-panelfooter clearfix"lp>',
            "initComplete": function (settings, json) {
                $('.express').on('click', function () {
                    var $span = $(this);
                    var str=prompt("请输入快递单号");
                    if(str){
                        $.ajax({
                            url:"points/addPointsExpressNumbers",
                            data:{id:$span.attr("id"),expressNumbers:str},
                            type:"POST",
                            async:false,
                            success:function(res){
                                if(res.success){
                                    $span.parent().prev().html("已发货");
                                    $span.parent().html(str);
                                    alert("添加成功");
                                }else{
                                    alert("添加失败");
                                }
                            }
                            
                        });
                    }
                });
            }
        });
        
        /**
         * 搜索按钮响应事件
         */
        $('#queryRepayBtn').on('click', function () {
            table.ajax.reload();
        });
        
    };
    initDateRangePicker();
    initTable();
});


